<template>
  <el-container id="panel">
    <el-header class="panel-head">
      <div class="panel-head-content">
        <div class="panel-head-row">
          <div class="panel-head-left">
            <div class="lh-logo">
              <img src="/logo.png" alt="">
            </div>
            <div class="lh-text">
              领航工作室
            </div>
          </div>
          <div class="panel-head-right">
            <div class="panel-user-box">
              <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                  <div class="user-avatar"></div>
                  <span class="user-name">{{ userBox.user.name }}</span>
                  <el-icon class="el-icon--right">
                    <arrow-down />
                  </el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item :icon="CirclePlus" command="changeInfo"
                      v-if="ModifyOwnPermissions">修改资料</el-dropdown-item>
                    <el-dropdown-item :icon="CirclePlus" command="logout">退出登录</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </div>
          </div>
        </div>
      </div>
    </el-header>
    <el-container class="panel-body">
      <el-aside class="panel-left" width="200px">
        <el-menu class="navigate-menu" @open="handleOpen" @close="handleClose" router>
          <el-menu-item :index="`/user/${userBox.user.id}`">
            <el-icon>
              <User />
            </el-icon>
            <span>个人主页</span>
          </el-menu-item>
          <el-menu-item index="/info">
            <el-icon>
              <Bell />
            </el-icon>
            <span>消息</span>
          </el-menu-item>
          <el-menu-item index="/projects">
            <el-icon>
              <Folder />
            </el-icon>
            <span>项目</span>
          </el-menu-item>
          <el-menu-item index="/technologies">
            <el-icon>
              <Key />
            </el-icon>
            <span>技术栈</span>
          </el-menu-item>
          <el-menu-item index="/users">
            <el-icon>
              <Connection />
            </el-icon>
            <span>成员</span>
          </el-menu-item>
          <el-menu-item index="/department">
            <el-icon>
              <HelpFilled />
            </el-icon>
            <span>部门</span>
          </el-menu-item>
          <el-menu-item index="/control">
            <el-icon>
              <Setting />
            </el-icon>
            <span>管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-scrollbar class="panel-main-scrollbar">
        <el-main class="panel-main">
          <router-view></router-view>
        </el-main>
      </el-scrollbar>
    </el-container>
  </el-container>
</template>

<script>
import { getUser, logoutUser } from '../../utils/user'
export default {
  data() {
    return {
      userBox: {
        user: {
          name: '未登录',
          id: ''
        }
      },
      pathWatchArr: ['user', 'info', 'projects', 'technologies', 'users', 'department', 'control']
    }
  },
  methods: {
    handleCommand(key) {
      if (key === 'logout') {
        logoutUser()
        this.$router.push({ path: '/login' })
      }
      if (key === 'changeInfo') {
        this.$router.push({ path: '/user/info' })
      }
    }
  },
  computed: {
    path() {
      return this.$route.path.split('/')[1]
    },
    ModifyOwnPermissions() {
      return this.usePermission.systemPermission.includes(1) || this.usePermission.systemPermission.includes(5)
    }
  },
  watch: {
    path: {
      handler(newVal, oldVal) {
        let newPath = this.pathWatchArr.includes(newVal)
        let oldPath = this.pathWatchArr.includes(oldVal)
        // console.log(newVal,oldVal);
        if (newPath && oldPath) {
          let oldIsActive = document.querySelector('.is-active')
          oldIsActive?.classList.remove('is-active')
          document.querySelectorAll('.el-menu-item')[this.pathWatchArr.indexOf(newVal)].classList.add('is-active')
        }
      }
    }
  },

  mounted() {
    var that = this
    getUser(function (user) {
      that.userBox.user = user
      that.usePermission.id = user.id
    })
    this.usePermission.getPermissionList()
  }
}
</script>

<style scoped>
#panel {
  width: 100%;
  height: 100%;
  /* background: linear-gradient(125deg, #64e67a, #ff8b4c); */
  background-color: #cccccc;
}

.panel-head {
  border-bottom: 1px solid #e8e8e8;
  background-color: rgba(255, 255, 255, 0.8);
}

.panel-head-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: row;
  align-items: center;
}

.panel-head-row {
  display: flex;
  width: 100%;
}

.panel-head-left {
  display: flex;
  align-items: center;
  width: 50%;
}

.lh-logo {
  width: 32px;
  height: 32px;
  background-color: black;
  box-sizing: border-box;
  padding: 5px;
  border-radius: 6px;
  margin-right: 16px;
}

.lh-logo img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.lh-text {
  font-size: 18px;
  color: black;
  letter-spacing: 2px;
}

.panel-head-right {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  width: 50%;
}

.panel-left {
  border-right: 1px solid #e8e8e8;
  background-color: rgba(255, 255, 255, 0.8);
}

.panel-body {
  overflow: hidden;
}

.navigate-menu {
  --el-menu-bg-color: rgba(0, 0, 0, 0);
  --el-menu-hover-bg-color: rgba(255, 255, 255, 0.6);
  --el-menu-border-color: none;
}

.panel-main-scrollbar {
  width: 100%;
  background-color: #ffffff;
}

.panel-main {
  width: 100%;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
}

.user-avatar {
  display: inline-block;
  width: 30px;
  height: 30px;
  margin-right: 8px;
  border-radius: 100px;
  background-color: #cccccc;
  vertical-align: middle;
}


.el-dropdown-link:focus {
  /* 去除下拉菜单组件默认黑框 */
  outline: none;
}

.panel-user-box {
  display: flex;
  flex-direction: row;
  align-items: center;
}
</style>
